<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名称" prop="carUser">
        <el-input
          v-model="queryParams.carUser"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车场名称" prop="chid">
        <el-select v-model="queryParams.chid" clearable>
          <el-option
            v-for="item in carsHome"
            :key="item.chid"
            :label="item.chname"
            :value="item.chid"
            @keyup.enter.native="handleQuery">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pxy-staff:car:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="carList">
      <el-table-column label="序号" align="center" prop="carid" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名称" align="center" prop="carUser" />
      <el-table-column label="手机号" align="center" prop="carTel" />
      <el-table-column label="所属车场" align="center" prop="carsHome.chname" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleallot"
          >车场分配</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pxy-staff:car:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pxy-staff:car:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车场人员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名称" prop="carUser">
          <el-input v-model="form.carUser" placeholder="请输入用户名称" />
        </el-form-item>
        <el-form-item label="手机号" prop="carTel">
          <el-input v-model="form.carTel" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="所属车场" prop="chid">
          <el-select v-model="form.chid" clearable>
            <el-option
              v-for="item in carsHome"
              :key="item.chid"
              :label="item.chname"
              :value="item.chid"
              @keyup.enter.native="handleQuery">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
import { listCar, getCar, delCar, addCar, updateCar } from "@/api/pxy-staff/car";
import {listHome} from "@/api/pxy-cars/home";
import {getCars} from "@/api/pxy-cars/cars";

export default {
  name: "Car",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车场人员表格数据
      carList: [],
      carsHome:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carUser: null,
        chid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        carUser: [
          { required: true, message: "用户名称不能为空", trigger: "blur" }
        ],
        carTel: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          { pattern: /^1[3456789]\d{9}$/, message: '手机格式错误', trigger: 'blur'}
        ],
        chid: [
          { required: true, message: "所属车场不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getHome();
  },
  methods: {
    /** 查询车场人员列表 */
    getList() {
      this.loading = true;
      listCar(this.queryParams).then(response => {
        this.carList = response.rows;
        console.log(this.carList)
        this.total = response.total;
        this.loading = false;
      });
    },
    getHome(){
      this.loading= true;
      listHome(this.queryParams).then(response=>{
        this.carsHome=response.rows;
        // console.log(this.carsHome)
        this.loading = false;
      })
    },
    handleallot(){
      this.$router.push({
        path:'indexallot'
      })
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        carid: null,
        code: null,
        status: null,
        deleted: null,
        createTime: null,
        updateTime: null,
        carUser: null,
        carTel: null,
        chid: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车场人员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const carid = row.carid || this.ids
      getCar(carid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车场人员";
      });
    },
    isValid(carTel){
      const regex = /^1[3456789]\d{9}$/;
      return regex.test(carTel);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.carid != null) {
            updateCar(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCar(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const carids = row.carid || this.ids;
      this.$modal.confirm('是否确认删除车场人员编号为"' + carids + '"的数据项？').then(function() {
        return delCar(carids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pxy-staff/car/export', {
        ...this.queryParams
      }, `car_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
